//app.js
App({
  onLaunch: function () {
    var that = this;
    var util = require('./utils/request.js');
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    //查看授权
    wx.getSetting({
      success: function (res) {
        //授权地理位置
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: function (res) {
              console.log('授权地理位置成功！')
            }
          })
        }
      }
    })
    // 检查是否授权用户信息
    var userInfo = wx.getStorageSync('XPuserInfo');
    if (userInfo == '') {
      wx.redirectTo({
        url: './pages/authorization/authorization',
      })
    } else {
      // 已授权
      // 检查是否登录
      if (this.globalData.memberId == null) {
        // wx.showLoading({
        //   title: '正在加载...',
        //   mask: true
        // })
        // 登录
        wx.login({
          success: function (res) {
            if (res.code) {
              var code = res.code;
              
              // 获取memberId
              util.req('/member/login', { 'code': code, 'encryptedData': userInfo.encryptedData, 'iv': userInfo.iv }, function (success) {
                if (success.resultCode == 200) {
                  that.globalData.memberId = success.resultContent.memberId;
                  var memberId = that.globalData.memberId;
                  console.log(memberId);
                  // 获取用户信息
                  util.req('/member/detail', { 'memberId': memberId }, function (res) {
                    console.log(res);
                    if (res.resultCode == 200) {
                      // 格式化余额，保留小数点后两位
                      res.resultContent.balance = (res.resultContent.balance).toString();
                      if (res.resultContent.balance.indexOf('.') == -1) {
                        res.resultContent.balance = res.resultContent.balance + '.00';
                      } else if (res.resultContent.balance.charAt(res.resultContent.balance.length - 2) == '.') {
                        res.resultContent.balance = res.resultContent.balance + '0'
                      }
                      that.globalData.userInfo = res.resultContent;
                    } else if (res.resultCode == 400) {
                      wx.showToast({
                        title: res.resultMsg,
                        icon: 'none'
                      })
                    } else {
                      wx.showToast({
                        title: '获取用户信息错误！',
                        icon: 'none'
                      })
                    }
                  })
                  wx.hideLoading();
                } else if (res.resultCode == 400) {
                  wx.showToast({
                    title: res.resultMsg,
                    icon: 'none'
                  })
                } else {
                  wx.showToast({
                    title: '登录错误！',
                    icon: 'none'
                  })
                }
              })
            }
          }
        })
      }
    }
  },
  globalData: {
    userInfo: null,
    memberId:null,
  }
})